import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_spread_props } from "@swc/helpers/_/_object_spread_props";
import _useMediaQuery from "@mui/material/useMediaQuery";
import React, { useMemo } from "react";
import classes from "./toast.module.less";
import Overlay from "../overlay";
import { render as reactRender, unmount as reactUnmount } from "rc-util/lib/React/render";
import { createPortal } from "react-dom";
import images from "../../assets";
import { KEYCONST } from "../../enums";
import classNames from "classnames";
import i18n, { LocalKeyMap } from "../../local";
import { walletRootRef } from "../../utils";
import { jsx as _jsx } from "react/jsx-runtime";
import { jsxs as _jsxs } from "react/jsx-runtime";
var Wrap = function(props) {
    return /*#__PURE__*/ _jsx("div", _object_spread({}, props));
};
function ToastJSX(props) {
    var theme = localStorage.getItem(KEYCONST.theme) || "dark";
    var isPc = _useMediaQuery("(min-width:1280px)");
    var clientRect = useMemo(function() {
        var _walletRootRef_current;
        return (_walletRootRef_current = walletRootRef.current) === null || _walletRootRef_current === void 0 ? void 0 : _walletRootRef_current.getBoundingClientRect();
    }, []);
    var getRootEleCenterStyle = function() {
        if (!isPc) {
            return undefined;
        }
        if (!(clientRect === null || clientRect === void 0 ? void 0 : clientRect.x) || !clientRect.y || !clientRect.width || !clientRect.height) {
            return {
                left: innerWidth / 2,
                top: innerHeight / 2,
                transform: "translate(-50%,-50%)",
                position: "absolute",
                height: "fit-content",
                display: "flex",
                justifyContent: "center",
                alignItems: "center",
                zIndex: 1400
            };
        }
        return {
            left: "50%",
            top: "50%",
            transform: "translate(-50%,-50%)",
            position: "absolute",
            height: "fit-content",
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
            zIndex: 1400
        };
    };
    var FlexWrap = isPc ? Wrap : React.Fragment;
    var _props_maskZIndex;
    return /*#__PURE__*/ createPortal(/*#__PURE__*/ _jsxs("div", {
        className: "".concat(classes.toast, " ").concat(props.centered ? classes.toast_center : ""),
        style: isPc ? {
            justifyContent: "normal",
            alignItems: "normal"
        } : {},
        children: [
            /*#__PURE__*/ _jsx(Overlay, {
                zIndex: props.maskZIndex,
                fullScreen: false,
                show: props.mask
            }),
            /*#__PURE__*/ _jsx(FlexWrap, _object_spread_props(_object_spread({}, isPc ? {
                style: getRootEleCenterStyle()
            } : undefined), {
                children: /*#__PURE__*/ _jsx("div", {
                    "data-theme": theme || "light",
                    className: "king-wallet-toastContent, ".concat(classes.toast_content),
                    style: {
                        zIndex: ((_props_maskZIndex = props.maskZIndex) !== null && _props_maskZIndex !== void 0 ? _props_maskZIndex : 1400) + 1
                    },
                    children: props.children
                })
            }))
        ]
    }), props.container);
}
var ToastInnerJSX = function(props) {
    var theme = localStorage.getItem(KEYCONST.theme) || "dark";
    return /*#__PURE__*/ createPortal(/*#__PURE__*/ _jsxs("div", {
        style: props.maskZIndex ? {
            zIndex: props.maskZIndex + 1,
            backgroundColor: props.maskColor
        } : undefined,
        className: "".concat(classes.toast_inner, " ").concat(props.centered ? classes.toast_center : ""),
        children: [
            /*#__PURE__*/ _jsx(Overlay, {
                zIndex: props.maskZIndex,
                show: props.mask,
                fullScreen: false,
                container: props.container
            }),
            /*#__PURE__*/ _jsx(Wrap, {
                className: "toast_wrapper",
                style: {
                    zIndex: 1
                },
                children: /*#__PURE__*/ _jsx("div", {
                    "data-theme": theme || "light",
                    className: "king-wallet-toastContent ".concat(classes.toast_content),
                    children: props.children
                })
            })
        ]
    }), props.container);
};
var toast = function(props) {
    var container = document.createDocumentFragment();
    var currentConfig = _object_spread_props(_object_spread({}, props), {
        container: props.container || walletRootRef.current || document.body
    });
    var ToastComponent = props.inner ? ToastInnerJSX : ToastJSX;
    var render = function(renderProps) {
        setTimeout(function() {
            reactRender(/*#__PURE__*/ _jsx(ToastComponent, _object_spread_props(_object_spread({}, renderProps), {
                children: renderProps.content
            })), container);
        });
    };
    var update = function(updateProps) {
        render(_object_spread({}, currentConfig, updateProps));
    };
    var destroy = function() {
        reactUnmount(container);
        (props === null || props === void 0 ? void 0 : props.afterClose) && (props === null || props === void 0 ? void 0 : props.afterClose());
    };
    render(currentConfig);
    if (props.duration !== -1) {
        setTimeout(destroy, props.duration || 2000);
    }
    return {
        destroy: destroy,
        update: update
    };
};
toast.confirm = function(props) {
    var destroy = function destroy() {
        toastDestroy();
    };
    var title = props.title, onCancel = props.onCancel, onConfirm = props.onConfirm, style = props.style, className = props.className;
    var _toast = toast({
        mask: true,
        centered: true,
        duration: -1,
        content: /*#__PURE__*/ _jsxs("div", {
            className: classes["toast_confirm"],
            style: style,
            children: [
                /*#__PURE__*/ _jsx("div", {
                    className: classes.title,
                    children: /*#__PURE__*/ _jsx("div", {
                        className: "".concat(classes.content, " ").concat(className || ""),
                        children: title
                    })
                }),
                /*#__PURE__*/ _jsxs("div", {
                    className: classes.btns,
                    children: [
                        /*#__PURE__*/ _jsx("div", {
                            onClick: onCancel || destroy,
                            className: "".concat(classes.btn, " ").concat(classes.cancel),
                            children: /*#__PURE__*/ _jsx("span", {
                                children: i18n.t(LocalKeyMap["toast.cancel"])
                            })
                        }),
                        /*#__PURE__*/ _jsx("div", {
                            onClick: onConfirm,
                            className: "".concat(classes.btn, " ").concat(classes.submit),
                            children: /*#__PURE__*/ _jsx("span", {
                                children: i18n.t(LocalKeyMap["toast.confirm"])
                            })
                        })
                    ]
                })
            ]
        })
    }), toastDestroy = _toast.destroy, update = _toast.update;
    return {
        destroy: destroy,
        update: update
    };
};
toast.point = function(props) {
    var destroy = function destroy() {
        toastDestroy();
    };
    var title = props.title, onOk = props.onOk;
    var _toast = toast({
        mask: true,
        centered: true,
        duration: -1,
        content: /*#__PURE__*/ _jsxs("div", {
            className: classes["toast_confirm"],
            children: [
                /*#__PURE__*/ _jsx("div", {
                    className: classes.title,
                    children: /*#__PURE__*/ _jsx("div", {
                        className: classes.content,
                        children: title
                    })
                }),
                /*#__PURE__*/ _jsx("div", {
                    className: classes.btns,
                    children: /*#__PURE__*/ _jsx("div", {
                        onClick: function() {
                            destroy();
                            onOk === null || onOk === void 0 ? void 0 : onOk();
                        },
                        className: classes.btn,
                        children: /*#__PURE__*/ _jsx("span", {
                            children: i18n.t(LocalKeyMap["toast.ok"])
                        })
                    })
                })
            ]
        })
    }), toastDestroy = _toast.destroy;
    return {
        destroy: destroy
    };
};
toast.copy = function() {
    var destroy = function destroy() {
        toastDestroy();
    };
    var _toast = toast({
        duration: 1500,
        centered: true,
        content: /*#__PURE__*/ _jsxs("div", {
            className: classes.copied,
            children: [
                i18n.t(LocalKeyMap["toast.copy"]),
                "!"
            ]
        })
    }), toastDestroy = _toast.destroy;
    return {
        destroy: destroy
    };
};
toast.common = function(props) {
    var destroy = function destroy() {
        toastDestroy();
    };
    var _toast = toast({
        duration: 3000,
        centered: true,
        content: /*#__PURE__*/ _jsx("div", {
            className: classNames(classes.common, props.className),
            children: props.title
        })
    }), toastDestroy = _toast.destroy;
    return {
        destroy: destroy
    };
};
var ToastComponent = function(props) {
    var _props_message = props.message, message = _props_message === void 0 ? "Password change successful" : _props_message, icon = props.icon;
    return /*#__PURE__*/ _jsx("div", {
        className: classes.page,
        children: /*#__PURE__*/ _jsxs("div", {
            className: classes.contain,
            children: [
                icon && /*#__PURE__*/ _jsx("img", {
                    src: icon === "success" ? images.paySuccess : images.payFail,
                    className: classes.success
                }),
                /*#__PURE__*/ _jsx("div", {
                    className: classes.text,
                    children: message
                })
            ]
        })
    });
};
var ErrorComponent = function(props) {
    var _props_message = props.message, message = _props_message === void 0 ? "Try later" : _props_message;
    return /*#__PURE__*/ _jsx("div", {
        className: classes.page,
        children: /*#__PURE__*/ _jsxs("div", {
            className: classes.errContain,
            children: [
                /*#__PURE__*/ _jsx("img", {
                    src: images.err,
                    className: classes.err
                }),
                /*#__PURE__*/ _jsx("div", {
                    className: classes.errText,
                    children: message
                })
            ]
        })
    });
};
toast.createToast = function(param) {
    var message = param.message, icon = param.icon, _param_type = param.type, type = _param_type === void 0 ? "toast" : _param_type;
    var getRenderComponent = function getRenderComponent() {
        switch(type){
            case "toast":
                return /*#__PURE__*/ _jsx(ToastComponent, {
                    message: message,
                    icon: icon
                });
            case "errToast":
                return /*#__PURE__*/ _jsx(ErrorComponent, {
                    message: message
                });
            default:
                return /*#__PURE__*/ _jsx(ToastComponent, {
                    message: message,
                    icon: icon
                });
        }
    };
    var destroy = function destroy() {
        toastDestroy();
    };
    var _toast = toast({
        mask: false,
        centered: true,
        content: getRenderComponent(),
        duration: 3000
    }), toastDestroy = _toast.destroy, update = _toast.update;
    return {
        destroy: destroy,
        update: update
    };
};
export default toast;

 //# sourceMappingURL=index.js.map